const http = require('http')
const fs = require('fs')
const path = require('path')

//创建 web服务器
const server = http.createServer()

server.on('request',(req, res) =>{
    //获取到客户端请求的 url 地址
    const url = req.url

    let fpath = ''
    if(url == '/') fpath = path.join(__dirname, './clock/index.html')
    else fpath = path.join(__dirname, '/clock', url)

    // 根据 ’映射‘ 过来的文件路径读取文件的内容
    fs.readFile(fpath, 'utf8', (err, data)=>{
        if(err) return res.end('404 Not found')
        res.end(data)
    })
})
//启动服务器
server.listen(80, ()=>{
    console.log('server runing at http://127.0.0.1');
})